package com.test.burnincash.util_burnin.http_burnin;

import static com.test.burnincash.constant_burnin.Url_burnin.*;

import android.text.TextUtils;

import com.test.burnincash.activity_fragment_burnin.MainActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.ServiceFeedbackActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseInfoActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseLoginActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.info_burnin.InfoUploadActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.loan_burnin.AddPaystackBankAccountActivity_Niri_burnin;
import com.test.burnincash.activity_fragment_burnin.loan_burnin.LoanApplyActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.loan_burnin.LoanMainActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.loan_burnin.RepayPaystackActivity_Niri_burnin;
import com.test.burnincash.bean_burnin.config_info_burnin.AppShowInfo_burnin;
import com.test.burnincash.bean_burnin.config_info_burnin.UrlInfo_burnin;
import com.test.burnincash.bean_burnin.process_burnin.ProcessInfo_burnin;
import com.test.burnincash.bean_burnin.user_info_burnin.LoanApplyDetailInfo_burnin;
import com.test.burnincash.bean_burnin.user_info_burnin.LoanLimitInfo_burnin;
import com.test.burnincash.bean_burnin.user_info_burnin.LoginInfo_burnin;
import com.test.burnincash.bean_burnin.user_info_burnin.NiriBankAccountList_burnin;
import com.test.burnincash.bean_burnin.user_info_burnin.RepayInfo_burnin;
import com.test.burnincash.dialog_burnin.LoadingDialog_burnin;
import com.test.burnincash.util_burnin.StringUtil_burnin;
import com.test.burnincash.util_burnin.analytics_burnin.event_burnin.IAnalyticsEvent_burnin;
import com.test.burnincash.util_burnin.analytics_burnin.util_burnin.AnalyticsUtil_burnin;

import org.json.JSONArray;
import org.json.JSONObject;

public class HttpPresenter_burnin {

    private static class Inner {
        private static final HttpPresenter_burnin instance = new HttpPresenter_burnin();
    }

    private HttpPresenter_burnin() {}

    public static HttpPresenter_burnin getInstance() {
        return Inner.instance;
    }

    private BaseActivity_burnin activity;

    public LoadingDialog_burnin progressBarDialog;

    public void setActivity(BaseActivity_burnin activity) {
        if (progressBarDialog != null && progressBarDialog.isShowing()) {
            progressBarDialog.dismiss();
        }
        if (activity != null) progressBarDialog = new LoadingDialog_burnin(activity);
        this.activity = activity;
    }

    public void progressBarDialogShow() {
        if (progressBarDialog == null) return;
        progressBarDialog.show();
    }

    public void progressBarDialogDismiss() {
        if (progressBarDialog == null) return;
        progressBarDialog.dismiss();
    }

    public void showToastOnUiThread(String text) {
        if (activity != null) {
            activity.showToastOnUiThread_burnin(text);
        }
    }

    //通知Activity处理响应
    public void notifyActivityHandleResponse(String word, JSONObject responseJson) {
        if (activity == null) return;
        try {
            String gcode = responseJson.optString("code");
            String gmsg = StringUtil_burnin.getSafeString(responseJson.optString("msg"));
            Object gobj = responseJson.opt("obj");

            //失败响应
            if (StringUtil_burnin.safeParseInt(gcode)!=0) {
                onResponseFail(word,gmsg);
                return;
            }

            //成功响应
            onResponseSuccess(word,gmsg,gobj);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //成功响应
    private void onResponseSuccess(String word,String gmsg,Object gobj){
        if (gobj instanceof JSONObject) {
            JSONObject objJsonObject = (JSONObject) gobj;
            String Upload_Info_Url = HttpManager_burnin.getInstance().getUpload_Info_Url();
            if (word.equalsIgnoreCase(Upload_Info_Url)|| (PATH_GLOBAL+word).equalsIgnoreCase(Upload_Info_Url)){
                if (activity instanceof BaseInfoActivity_burnin) ((BaseInfoActivity_burnin) activity).onInfoUploadResponse_burnin();
                return;
            }
            if (word.equalsIgnoreCase(PATH_REQUEST_URLS)){//获取Url
                UrlInfo_burnin.parse(objJsonObject);
                if (activity instanceof BaseLoginActivity_burnin) ((BaseLoginActivity_burnin)activity).onRequestUrlsResponse();
                return;
            }
            if (word.equalsIgnoreCase(PATH_APP_SHOW_INFO)){//获取配置
                AppShowInfo_burnin.parse(objJsonObject);
                if (activity instanceof MainActivity_burnin) ((MainActivity_burnin) activity).onAppShowInfoResponse();
                else if (activity instanceof LoanMainActivity_burnin) ((LoanMainActivity_burnin) activity).onAppShowInfoResponse();
                return;
            }
            if (word.equalsIgnoreCase(PATH_SMS_CODE)){//获取验证码
                if (activity instanceof BaseLoginActivity_burnin) ((BaseLoginActivity_burnin) activity).onRequestOtpCodeResponse_burnin(gmsg,objJsonObject);
                return;
            }
            if (word.equalsIgnoreCase(PATH_USER_LOGIN)){//登录
                LoginInfo_burnin.parse(objJsonObject);
                if (activity instanceof BaseLoginActivity_burnin) ((BaseLoginActivity_burnin) activity).onUserLoginResponse_burnin();
                return;
            }
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getNft())){//提交反馈
                if (activity instanceof ServiceFeedbackActivity_burnin) ((ServiceFeedbackActivity_burnin) activity).onSubmitServiceFeedback();
                return;
            }
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getHrt())){//用户流程状态
                ProcessInfo_burnin.parse(objJsonObject.toString());
                activity.onUserProcessResponse_burnin();
                return;
            }
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getGr3())){//额度信息
                LoanLimitInfo_burnin.parse(objJsonObject);
                if (activity instanceof LoanMainActivity_burnin) ((LoanMainActivity_burnin) activity).onLoanLimitInfoResponse();
                return;
            }
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getJhk())){//借款页信息
                LoanApplyDetailInfo_burnin.parse(objJsonObject);
                if (activity instanceof LoanMainActivity_burnin) ((LoanMainActivity_burnin) activity).onLoanApplyDetailResponse();
                else if (activity instanceof BaseInfoActivity_burnin) ((BaseInfoActivity_burnin) activity).onLoanApplyDetailResponse_burnin();
                else if (activity instanceof LoanApplyActivity_burnin) ((LoanApplyActivity_burnin) activity).onLoanApplyDetailResponse();
                return;
            }
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getRfi())){//发起借款申请响应
                AnalyticsUtil_burnin.INSTANCE.addEvent(IAnalyticsEvent_burnin.AT_LOAN_APPLY);
                if (activity instanceof LoanApplyActivity_burnin) ((LoanApplyActivity_burnin) activity).onLoanApplyResponse();
                return;
            }
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getLhj())){//还款信息展示
                RepayInfo_burnin.parse(objJsonObject);
                if (activity instanceof LoanMainActivity_burnin) ((LoanMainActivity_burnin) activity).onRepayDetailResponse();
                return;
            }
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getOgh())){//线上还款-初始化paystack订单（尼日）
                if (activity instanceof AddPaystackBankAccountActivity_Niri_burnin) ((AddPaystackBankAccountActivity_Niri_burnin) activity).onOrderRepaySDKResponse(objJsonObject);
                else if (activity instanceof RepayPaystackActivity_Niri_burnin) ((RepayPaystackActivity_Niri_burnin) activity).onOrderRepaySDKResponse(objJsonObject);
                return;
            }
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getBhr())){//线下还款-创建虚拟账号还款（尼日）
                if (activity instanceof LoanMainActivity_burnin) ((LoanMainActivity_burnin) activity).onInitOfflinePayChannelResponse();
                return;
            }
        } else if (gobj instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) gobj;
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getF5r())){//获取银行列表（尼日）
                if (activity instanceof InfoUploadActivity_burnin) ((InfoUploadActivity_burnin) activity).onFetchBankCodeResponse(jsonArray);
                return;
            }
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getJtw())){//获取paystack卡列表
                NiriBankAccountList_burnin.parse(jsonArray);
                if (activity instanceof RepayPaystackActivity_Niri_burnin) ((RepayPaystackActivity_Niri_burnin) activity).onFetchBankListResponse();
                else if (activity instanceof LoanMainActivity_burnin) ((LoanMainActivity_burnin) activity).onFetchBankListResponse_Niri();
                return;
            }
        } else {
            //其它类型
            gmsg = StringUtil_burnin.getSafeString(gmsg);
            if (TextUtils.isEmpty(gmsg)) {
                gmsg = "Response something error";
            }
            activity.showToast_burnin(gmsg);
        }
    }

    //失败响应
    private void onResponseFail(String word,String gmsg){
        activity.showToast_burnin(gmsg);
//        switch (word){}
    }

}
